package com.passive.lock.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.route.RouteService;
import com.passive.lock.service.user.DeptService;
import com.passive.lock.service.user.RegionService;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("")
public class TreeController extends BaseController {
	@Autowired
	private DeptService deptService;
	@Autowired
	private RegionService regionService;
	@Autowired
	private RouteService routeService;

	@RequestMapping(value = "/grouptree")
	@ResponseBody
	public Result groupTree(HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = deptService.groupTree(tUserVo.getGroupid());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}

	@RequestMapping(value = "/depttree")
	@ResponseBody
	public Result deptTree(@RequestParam String companyId, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = deptService.deptTree(companyId);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}

	@RequestMapping(value = "/companytree")
	@ResponseBody
	public Result companyTree(HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = deptService.companyTree(tUserVo.getGroupid());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}

	@RequestMapping(value = "/regiontree")
	@ResponseBody
	public Result regionTree(@RequestParam(required = false) Integer regionid, HttpServletRequest req,
			HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(regionid == null||"".equals(regionid)){
		regionid = tUserVo.getRegionid();
		}
		try {
			Result result = regionService.regionTree(regionid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}

	@RequestMapping(value = "/regionchildrentree")
	@ResponseBody
	public Result regionChildrenTree(@RequestParam(required = false) Integer parentid, HttpServletRequest req,
			HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = regionService.childrenRegion(parentid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}

	@RequestMapping(value = "/querymaptree")
	@ResponseBody
	public Result queryMapTree(@RequestParam(required = false) String pid,
			@RequestParam(required = false) Integer layer, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		String contents_pic = tUserVo.getGroupids().substring(1, tUserVo.getGroupids().length() - 1);
		String[] a = contents_pic.split(",");
		Result result = null;
		if (layer < (a.length - 1)) {
			pid = a[layer + 1].trim();
			pid = pid.substring(1, pid.length() - 1);
			result = deptService.queryMapTree1(pid);
		} else {
			result = deptService.queryMapTree2(pid);
		}

		return result;
	}
	
	
	@RequestMapping(value = "/routetree")
	@ResponseBody
	public Result routeTree(@RequestParam(required = false) Integer regionid, HttpServletRequest req,
			HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(regionid == null||"".equals(regionid)){
		regionid = tUserVo.getRegionid();
		}
		try {
			Result result = routeService.routeTree(regionid,tUserVo.getGroupid());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}
	
	@RequestMapping(value = "/grouptreebyid")
	@ResponseBody
	public Result groupTreeById(HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = deptService.groupTreeById(tUserVo.getGroupid());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, "服务异常,请联系管理员");
		}
	}
}
